import { createVueRouter, generateRouteModuleList, getWhiteRouteNames } from 'vc/src/router';
import type { App } from 'vue';
import { setupRouterGuard } from '@/router/router-guard';

import { routes } from './constants';

export const router = createVueRouter(routes, false);

/** 白名单应该包含基本静态路由 */
const WHITE_NAME_LIST: string[] = [];
getWhiteRouteNames(routes, WHITE_NAME_LIST);

export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !WHITE_NAME_LIST.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

const modules: ModulesDefaultType = import.meta.glob('./routes/**/*.ts', { eager: true });
const pro_modules: ModulesDefaultType = import.meta.glob(['./routes/business/sewagePermit.ts'], { eager: true });

// const modulesViews: ModulesDefaultType = import.meta.glob(['../../views/**/*.vue', '!../../views/**/modules/**/*.vue', '!../../views/apis/**/*.vue', '!../../views/apis/**/*.vue'], { eager: true });

export const asyncRoutes = [...generateRouteModuleList(process.env.NODE_ENV === 'production' ? modules : modules )];

console.log(asyncRoutes, 888);

export async function setupRouter (app: App<Element>) {
  app.use(router);
  setupRouterGuard(router);
  await router.isReady();
}
